using System;
using System.Collections.Generic;
using System.Text;
using MoEngine;
using Microsoft.Xna.Framework.Graphics;

namespace MoGameLib
{
    public class World : CoreComponent
    {
        public override void Initialize()
        {
            base.Initialize();
            Create();

            // create a human player
            m_HumanPlayer = new Player();
        }

        private Player m_HumanPlayer;

        public Player HumanPlayer
        {
            get { return m_HumanPlayer; }
        }


        protected virtual void Create()
        {
			CreateDamageTypes();
            CreateTerrains();
            CreateSiteTypes();

        }

		protected void CreateDamageTypes()
		{
			DamageTypes.CreateDamageTypes();
		}

        protected TerrainCollection m_Terrains = new TerrainCollection();

        public TerrainCollection Terrains
        {
            get { return m_Terrains; }
        }

        protected virtual void CreateTerrains()
        {
            Terrain forest = m_Terrains.Create("f");
            forest.Image = Core.Content.Load<Texture2D>("Images/Terrain/Forest");

            Terrain hill = m_Terrains.Create("n");
            hill.Image = Core.Content.Load<Texture2D>("Images/Terrain/Hill");
        }

        protected SiteTypeCollection m_SiteTypes = new SiteTypeCollection();

        public SiteTypeCollection SiteTypes
        {
            get { return m_SiteTypes; }
        }

        protected virtual void CreateSiteTypes()
        {
            foreach (Type type in GetType().Assembly.GetTypes())
            {
                if (type.IsSubclassOf(typeof(SiteType)))
                {
                    m_SiteTypes.Add((SiteType)Activator.CreateInstance(type));
                }
            }
        }

        protected Map m_CurrentMap;

        public Map CurrentMap
        {
            get { return m_CurrentMap; }
        }

        public virtual Map LoadMap(string mapFile)
        {
            m_CurrentMap = new Map();
            m_CurrentMap.Load(mapFile);
            return m_CurrentMap;
        }

        public virtual void LoadDemoMaps()
        {
            Map map = LoadMap("Content/Maps/Test.txt");
            // apply some settings to the map
            // tiles, structures
            Site CityAmn = new Site(SiteTypeNames.City, "Amn");
            map.GetTile(6, 3).Sites.Add(CityAmn);
            CityAmn.SubSites.Add(new Site(SiteTypeNames.Barrack));
            CityAmn.SubSites.Add(new Site(SiteTypeNames.SorceryTower));
        }
    }
}
